﻿using System.Collections.Generic;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{

    /// <summary>
    /// 标签类型表仓储
    /// </summary>
    public class TagtypeRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 新增
        /// </summary>
        public async Task<long> AddAsync(Tagtype model)
        {
            string insertSql = @"insert into `t_tagtype`
                                    (`group_id`,`name`,`create_id`,`create_time`,`update_time`,`remark`)
                                values
                                    (@GroupId,@Name,@CreateId,@CreateTime,@UpdateTime,@Remark);
                                select @@IDENTITY";
            return await mydb.ExecuteScalarAsync<long>(insertSql, model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long id)
        {
            string deleteSql = @"delete from `t_tagtype` where id = @Id;";
            return await mydb.ExecuteAsync(deleteSql, new { Id = id });
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<Tagtype> GetInfoAsync(long id)
        {
            string querySql = @"select
                                `id`, `group_id`, `name`, `create_id`, `create_time`, `update_time`, `remark`
                             from `t_tagtype`
                             where id = @Id;";
            return await mydb.GetFirstOrDefaultAsync<Tagtype>(querySql, new { Id = id });
        }

        /// <summary>
        /// 查询标签类型列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Tagtype>> GetListAsync(long groupId)
        {
            string querySql = @"select
                                `id`, `group_id`, `name`, `create_id`, `create_time`, `update_time`, `remark`
                             from `t_tagtype`
                             where group_id = @GroupId;";
            return await mydb.GetListAsync<Tagtype>(querySql, new { GroupId = groupId });
        }

        /// <summary>
        /// 查询标签类型列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Tagtype>> GetListByIdsAsync(List<long> ids)
        {
            string querySql = @"select
                                `id`, `group_id`, `name`, `create_id`, `create_time`, `update_time`, `remark`
                             from `t_tagtype`
                             where id IN @ids;";
            return await mydb.GetListAsync<Tagtype>(querySql, new { ids });
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateAsync(Tagtype model)
        {
            string updateSql = @"update
                                `t_tagtype`
                                set `name`=@Name, `update_time`=@UpdateTime, `remark`=@Remark
                                 where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, model);
        }

    }
}
